/*
 * @Description: 添加、编辑登记通用表单
 * @Author: Rfan
 * @Date: 2022-11-19 16:31:39
 * @LastEditTime: 2022-12-08 16:23:37
 */

import { useEffect, useState } from 'react';
import { Form, Input, Select } from 'antd';
import JPDatePicker from '@/components/JPComponents/JPDatePicker';
import { appState } from '@/store/mobx';
import { useBaseInfoContext } from '../../../contexts/BaseInfoContext';
import Fond from '@/domains/fond/entities/fond';
import ArchiveType from '@/domains/archiveType/entities/archiveType';
import fondService from '@/domains/fond/fondService';
import archiveTypeService from '@/domains/archiveType/archiveTypeService';
import type { IFond } from '@/interfaces/fond';
import type { IArchiveType } from '@/interfaces/archiveType';
import type { IBaseDic } from '@/interfaces/global';

interface IBaseInfoFormProps {
  visible: boolean;
}

const BaseInfoForm = (props: IBaseInfoFormProps) => {
  const { visible } = props;
  const {
    archiveBrickStatusList,
    breakageStatusList,
    pollutionStatusList,
    acidStatusList,
    damageStatusList
  } = useBaseInfoContext();
  const form = Form.useFormInstance();
  const selectFond = Form.useWatch('fondsId', form);

  // 全宗列表
  const [fondList, setFondList] = useState<IFond[]>([]);
  // 档案类型列表
  const [ATList, setATList] = useState<IArchiveType[]>([]);

  useEffect(() => {
    if (visible) {
      fondService.getFondList(appState.unitId).then((list) => {
        setFondList(list.map((item: Fond) => ({ value: item.id, label: item.fondName })));
      });
    }
  }, [visible]);

  useEffect(() => {
    if (selectFond) {
      archiveTypeService.getATList(selectFond).then((list) => {
        setATList(list.map((item: ArchiveType) => ({ value: item.id, label: item.atName })));
      });
    }
  }, [selectFond]);

  return (
    <>
      <Form.Item name="registrationTime" label="登记时间" rules={[{ required: true }]}>
        <JPDatePicker showTime />
      </Form.Item>
      {/* <Form.Item label="档号">
        <Input allowClear />
      </Form.Item> */}
      <Form.Item name="fondsId" label="全宗" rules={[{ required: true }]}>
        <Select options={fondList} />
      </Form.Item>
      <Form.Item name="archiveTypeId" label="档案类型" rules={[{ required: true }]}>
        <Select options={ATList} />
      </Form.Item>
      {/* <Form.Item label="临时号">
        <Input allowClear />
      </Form.Item> */}
      <Form.Item name="startTime" label="卷内文件开始时间">
        <JPDatePicker showTime />
      </Form.Item>
      <Form.Item name="endTime" label="卷内文件结束时间">
        <JPDatePicker showTime />
      </Form.Item>
      <Form.Item name="filePageCount" label="卷内文件张页数">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="isArchiveBrick" label="是否成为档案砖(数)">
        <Select
          options={archiveBrickStatusList.map((item: IBaseDic) => ({
            value: item.id,
            label: item.name
          }))}
        />
      </Form.Item>
      <Form.Item name="isWrapperBreakage" label="卷皮是否破损或变形（数）">
        <Select
          options={breakageStatusList.map((item: IBaseDic) => ({
            value: item.id,
            label: item.name
          }))}
        />
      </Form.Item>
      <Form.Item name="bindingForm" label="装帧形式">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="isBindingBreakage" label="装帧是否损毁（数）">
        <Select
          options={damageStatusList.map((item: IBaseDic) => ({
            value: item.id,
            label: item.name
          }))}
        />
      </Form.Item>
      <Form.Item name="isWrapperAcidate" label="卷皮是否酸化（数）">
        <Select
          options={acidStatusList.map((item: IBaseDic) => ({
            value: item.id,
            label: item.name
          }))}
        />
      </Form.Item>
      <Form.Item name="isSevereContamination" label="是否严重污染（数）">
        <Select
          options={pollutionStatusList.map((item: IBaseDic) => ({
            value: item.id,
            label: item.name
          }))}
        />
      </Form.Item>
      <Form.Item name="registrant" label="检查人">
        <Input allowClear />
      </Form.Item>
    </>
  );
};

export default BaseInfoForm;
